#include "list.h"

int 
initListHead(listHead *list)
{
	if(!list)
		return -1;
	list->headNode = NULL;
	list->tailNode = NULL;
	list->count = 0;

	return 0;
}

extern listNode * 
nextListNode(listNode *currentNode)
{	
	if(!currentNode)
	{
		return NULL;
	}
	return (currentNode->next);
}

extern listNode * 
prevListNode(listNode *currentNode)
{
	if(!currentNode)
	{
		return NULL;
	}
	return (currentNode->prev);
}

extern listNode * 
firstNode(listHead *list)
{
	if(!list)
	{
		return NULL;
	}
	return (list->headNode);
}

extern listNode * 
lastNode(listHead *list)
{
	if(!list)
	{
		return NULL;
	}
	return (list->tailNode);
}

extern void 			
addListNode(listNode *newNode, listNode *prev, listNode *next, listHead *list)
{
	if(!newNode || !prev || !next || !list)
		return ;
	prev->next = newNode;
	next->prev = newNode;
	newNode->next = next;
	newNode->prev = prev;
	
	list->count ++;
}

extern void 			
addListNodeHead(listNode *newNode, listHead *list)
{
	if(!list || !newNode)
		return;
	
	if(list->count == 0)
	{
		list->headNode = list->tailNode = newNode;
		newNode->prev = NULL;
		newNode->next = NULL;		
	}

	else
	{
		newNode->prev = NULL;
		newNode->next = list->headNode;
		
		list->headNode->prev = newNode;
		list->headNode = newNode;
	}

	list->count ++;
}

extern void			
addListNodeTail(listNode *newNode, listHead *list)
{
	if(!list || !newNode)
		return;
	if(list->count == 0)
	{
		list->tailNode = list->headNode = newNode;
		newNode->prev = NULL;
		newNode->next = NULL;	
	}

	else
	{
		newNode->next = NULL;
		newNode->prev = list->tailNode;
		
		list->tailNode->next = newNode;
		list->tailNode = newNode;
	}

	list->count++;
}

extern void			
_delListNode(listNode *prev, listNode *next, listHead *list)
{
	if(!prev || !next || !list)
		return ;
	prev->next = next;
	next->prev = prev;

	list->count -- ;
}

extern void			
delListNode(listNode *delNode, listHead *list)
{
	if(!delNode || !list)
		return;
	
	listNode *prev = delNode->prev;
	listNode *next = delNode->next;
	if(!prev && !next)
	{
		list->headNode = NULL;
		list->tailNode = NULL;
		list->count -- ;
		return;
	}

	if(!prev)
	{
		list->headNode = list->headNode->next;
		list->headNode->prev = NULL;
		list->count -- ;
		return;
	}

	if(!next)
	{
		list->tailNode = list->tailNode->prev;
		list->tailNode->next = NULL;
		list->count --;
		return;
	}

	_delListNode(prev, next, list);
	return;	
}

//DEF_LIST_WITH_TYPE(int, int, int);
//DEF_LIST_FUNCTIONS(int, int, int);


/*int main(int argc, char * argv[])
{
	int_list list;
	
	init_int_list(&list);

	int i = 0;
	while(i<20)
	{		
		list_node_int * ptr = (list_node_int *)malloc(sizeof(list_node_int));
		ptr->data = i;
		ptr-> key = i;
		
		printf("try to insert a node into list!\n");

		insert_into_int_list_tail(ptr, &list);

		list_node_int *tmp = get_first_int_node(&list);
		while(tmp)
		{
			printf("No.[%d] node, value = %d, list node count=%u\n", tmp->key, tmp->data, list.list_head.count);
			tmp = get_next_int_node(tmp);
		}


		i++;
		sleep(2);
		
	}

	int j = 0;
	for(; j<25; j++)
	{
		list_node_int * p = find_int_node(j, &list);
		if(p)
		{
			printf("find list node for j=%d, key=%d, value = %d, address=%p\n", j, p->key, p->data, p);
		}
		else
		{
			printf("can not find node for key=%d\n", j);
		}
	}

	while(i > 0)
	{
		list_node_int *ptr = get_last_int_node(&list);
		delete_int_node(ptr, &list);

		list_node_int *tmp = get_first_int_node(&list);
		while(tmp)
		{
			printf("No.[%d] node, value = %d, list no count=%u\n", tmp->key, tmp->data, list.list_head.count);
			tmp = get_next_int_node(tmp);
		}
		
		i--;
		sleep(2);
	}
	
}*/
